#include "cprogressbar.h"



CProgressBar::CProgressBar(QWidget *parent)
    : QSlider(Qt::Horizontal, parent),
      hovered(false),
      pressed(false),
      currentTheme(DefaultBlue) {
    setRange(0, 100);
    setValue(0);
    setMouseTracking(true);
    applyTheme(NeonBlue);
}

// 应用主题
void CProgressBar::applyTheme(Theme theme) {
    currentTheme = theme;
    switch (theme) {
        case DefaultBlue:
            applyDefaultBlueStyle();
            break;
        case NeonBlue:
            applyNeonBlueStyle();
            break;
        case IceBlue:
            applyIceBlueStyle();
            break;
        case DeepOcean:
            applyDeepOceanStyle();
            break;
    }
}

// 获取当前主题
CProgressBar::Theme CProgressBar::getCurrentTheme()
{
    return currentTheme;
}

void CProgressBar::mouseMoveEvent(QMouseEvent *event)
{
    QSlider::mouseMoveEvent(event);
    hovered = true;
    update();
}

void CProgressBar::leaveEvent(QEvent *event)
{
    QSlider::leaveEvent(event);
    hovered = false;
    update();
}

void CProgressBar::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        pressed = true;
        // 计算点击位置对应的值
        QStyleOptionSlider opt;
        initStyleOption(&opt);
        QRect grooveRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
        QRect handleRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);

        int sliderMin = grooveRect.x();
        int sliderMax = grooveRect.right() - handleRect.width() / 2;
        int newPosition = QStyle::sliderValueFromPosition(
            minimum(), maximum(),
            event->pos().x() - sliderMin,
            sliderMax - sliderMin,
            opt.upsideDown
        );

        setValue(newPosition);
        emit sliderMoved(newPosition);
        event->accept();
    }
    QSlider::mousePressEvent(event);
}

void CProgressBar::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        pressed = false;
        update();
    }
    QSlider::mouseReleaseEvent(event);
}

void CProgressBar::paintEvent(QPaintEvent *event)
{
    QSlider::paintEvent(event);

    // 自定义绘制悬停和按下效果
    if (hovered || pressed) {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        QStyleOptionSlider opt;
        initStyleOption(&opt);
        QRect handleRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);

        if (pressed) {
            // 绘制按下效果
            painter.setPen(QPen(QColor(60, 130, 220), 1));
            painter.setBrush(QColor(220, 230, 240, 150));
            painter.drawEllipse(handleRect.center(), 10, 10);
        } else if (hovered) {
            // 绘制悬停效果
            painter.setPen(QPen(QColor(70, 150, 255), 1));
            painter.setBrush(QColor(180, 210, 255, 100));
            painter.drawEllipse(handleRect.center(), 9, 9);
        }
    }
}

void CProgressBar::applyDefaultBlueStyle()
{
    setStyleSheet(
        "QSlider {"
        "   background-color: rgb(67,67,67);"  // 整体背景色
        "}"
        "QSlider::groove:horizontal {"
        "    background: qlineargradient(spread:pad, x1:0, y1:0.5, x2:1, y2:0.5,"
        "        stop:0 rgba(120, 140, 160, 100), stop:1 rgba(90, 110, 130, 150));"
        "    height: 3px;"
        "    border-radius: 1.5px;"
        "}"
        "QSlider::sub-page:horizontal {"
        "    background: qlineargradient(spread:pad, x1:0, y1:0.5, x2:1, y2:0.5,"
        "        stop:0 rgb(70, 150, 255), stop:1 rgb(40, 100, 220));"
        "    height: 3px;"
        "    border-radius: 1.5px;"
        "}"
        "QSlider::handle:horizontal {"
        "    background: qradialgradient(cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,"
        "        stop:0 rgb(245, 250, 255), stop:0.7 rgb(200, 220, 240), stop:1 rgb(180, 200, 220));"
        "    border: 1px solid rgb(100, 140, 180);"
        "    width: 16px;"
        "    height: 16px;"
        "    margin: -7px 0;"
        "    border-radius: 8px;"
        "}"
    );
}

void CProgressBar::applyNeonBlueStyle()
{
    setStyleSheet(
        "QSlider {"
        "   background-color: rgb(67,67,67);"  // 整体背景色
        "}"
        "QSlider::groove:horizontal {"
        "    background: rgba(30, 40, 60, 150);"
        "    height: 3px;"
        "    border-radius: 1.5px;"
        "}"
        "QSlider::sub-page:horizontal {"
        "    background: qlineargradient(spread:pad, x1:0, y1:0.5, x2:1, y2:0.5,"
        "        stop:0 rgb(0, 200, 255), stop:1 rgb(0, 150, 255));"
        "    height: 3px;"
        "    border-radius: 1.5px;"
        "}"
        "QSlider::handle:horizontal {"
        "    background: qradialgradient(cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,"
        "        stop:0 rgba(200, 250, 255, 200), stop:1 rgba(100, 200, 255, 150));"
        "    border: 1px solid rgb(0, 180, 255);"
        "    width: 16px;"
        "    height: 16px;"
        "    margin: -7px 0;"
        "    border-radius: 8px;"
        "}"
    );
}

void CProgressBar::applyIceBlueStyle()
{
    setStyleSheet(
        "QSlider {"
        "   background-color: rgb(67,67,67);"  // 整体背景色
        "}"
        "QSlider::groove:horizontal {"
        "    background: rgba(180, 200, 220, 100);"
        "    height: 2px;"
        "    border-radius: 1px;"
        "}"
        "QSlider::sub-page:horizontal {"
        "    background: qlineargradient(spread:pad, x1:0, y1:0.5, x2:1, y2:0.5,"
        "        stop:0 rgb(100, 200, 255), stop:1 rgb(70, 170, 240));"
        "    height: 2px;"
        "    border-radius: 1px;"
        "}"
        "QSlider::handle:horizontal {"
        "    background: qradialgradient(cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,"
        "        stop:0 rgb(250, 255, 255), stop:0.7 rgb(220, 240, 255), stop:1 rgb(200, 230, 250));"
        "    border: 1px solid rgb(150, 190, 220);"
        "    width: 14px;"
        "    height: 14px;"
        "    margin: -7px 0;"
        "    border-radius: 7px;"
        "}"
    );
}

void CProgressBar::applyDeepOceanStyle()
{
    setStyleSheet(
        "QSlider {"
        "   background-color: rgb(67,67,67);"  // 整体背景色
        "}"
        "QSlider::groove:horizontal {"
        "    background: qlineargradient(spread:pad, x1:0, y1:0.5, x2:1, y2:0.5,"
        "        stop:0 rgba(80, 100, 140, 120), stop:1 rgba(50, 70, 100, 180));"
        "    height: 4px;"
        "    border-radius: 2px;"
        "}"
        "QSlider::sub-page:horizontal {"
        "    background: qlineargradient(spread:pad, x1:0, y1:0.5, x2:1, y2:0.5,"
        "        stop:0 rgb(60, 140, 240), stop:1 rgb(30, 80, 190));"
        "    height: 4px;"
        "    border-radius: 2px;"
        "}"
        "QSlider::handle:horizontal {"
        "    background: qradialgradient(cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,"
        "        stop:0 rgb(230, 240, 255), stop:0.7 rgb(180, 200, 230), stop:1 rgb(150, 180, 210));"
        "    border: 1px solid rgb(80, 120, 160);"
        "    width: 18px;"
        "    height: 18px;"
        "    margin: -8px 0;"
        "    border-radius: 9px;"
        "}"
    );
}
